<?php
namespace app\index\controller;

use think\Controller;
use think\Db;

class User extends controller
{
    public function index()
    {
        return $this->fetch();
    }

    public function login()
    {
        $snumber = input('snumber');
        $password = input('password');
        $rs = Db::name("user")->where("sNumber", $snumber)
            ->where("password", md5($password))->find();
        if ($rs) {
            echo json_encode($rs);
        } else {
            echo 0;
        }
    }

    public function register()
    {
        $snumber = input('snumber');
        $password = input('password');
        $school = input('school');
        $teleNumber = input('teleNumber');
        $name = randCode();
        $check = Db::name("user")
            ->where("sNumber", $snumber)
            ->find();
        if (vailsNumber($snumber) && $password != null) {
            if ($check) {
                //    已存在该学号
                echo 3;
            } else {
                $data = [
                    'sNumber' => $snumber,
                    'password' => md5($password),
                    'name' => $name,
                    'schoolId' => $school,
                    'adddate' => date("Y-m-d"),
                    'teleNumber' => $teleNumber
                ];
                $res = Db::name("user")
                    ->insert($data);
                // dump($res);
                return $res;
            }
        } else {
            // 学号不正确
            echo 2;
        }
    }

    public function getMessage() {
        $uid = input('uid');
        $rs = Db::name('messages m')->join('user u','m.fromId=u.userId')
        ->join('school s','s.schoolId=u.schoolId')
        ->field('mId,mContent,fromId,mTime,sNumber,name,teleNumber,schoolName')
        ->where('toId',$uid)->select();
        if($rs){
            return json_encode($rs);
        }else{
            return 0;
        }
    }
    
    public function mycomments(){
        $uid = input('uid');
        $rs = Db::name('user u')->where('userId',$uid)
        ->join('comments c', 'u.userId=c.uId')
        ->join('goods g','g.gId=c.goodId')
        ->field('cId,cContent,gTitle,gId')
        ->where('cstatus',0)
        ->select();
        // dump($rs);
        return json_encode($rs);
    }

    public function mySave(){
        $uid = input('uid');
        $rs = Db::name('saved s')->where('userId',$uid)
        ->join('goods g', 's.gId=g.gId')
        ->select();
        return json_encode($rs);
    }

    public function checkSave(){
        $gid = input('gid');
        $uid = input('uid');
        $rs = Db::name('saved s')->where('userId',$uid)
        ->join('goods g', 's.gId=g.gId')
        ->where('g.gId',$gid)
        ->where('s.status',0)
        ->find();
        if($rs)
        {
            return 1;
        }else{
            return 0;
        }
    }

}
?>
<?php
function randCode($lenth = 6)
{
    $min = 0;
    $max = 1;
    $rand = $min + mt_rand() / mt_getrandmax()  * ($max - $min);
    $randCode = base_convert($rand, 10, 36);
    return substr($randCode, 2, $lenth);
}

function vailsNumber($snumber)
{
    if (is_numeric($snumber)) {
        $lenth = strlen($snumber);
        if ($lenth > 10 && $lenth < 15) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}
